package com.netease.nie.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.netease.nie.base.cache.redis.Redis;
import com.netease.nie.common.CommonService;
import com.netease.nie.constants.CachePrefixConstants;
import com.netease.nie.dao.DrawLotteryCreditGoodsInfoDao;
import com.netease.nie.model.DrawLotteryCreditGoodsInfo;

/**
 * 抽奖积分业务服务接口实现
 * @author gzlibingjie
 */
@Service
public class DrawLotteryCreditGoodsInfoServiceImpl extends CommonService implements DrawLotteryCreditGoodsInfoService {

	@Resource
	private DrawLotteryCreditGoodsInfoDao drawLotteryCreditGoodsInfoDao;
	
	@Autowired
	private Redis redis;
	
	private static final String DRAW_LOTTERY_CREDIT_GOODS_INFO_PREFIX = CachePrefixConstants.DrawLotteryPrefix.DRAW_LOTTERY_CREDIT_GOODS_INFO_PREFIX;
	
	/**
	 * 获取指定抽奖活动下的抽奖积分列表
	 * @param activityId 抽奖活动ID
	 * @return 抽奖积分列表
	 */
	@Override
	public List<DrawLotteryCreditGoodsInfo> listByActivityId(Integer activityId) {
		if (activityId == null) {
			return null;
		}
		return queryByActivityId(activityId);
	}

	/**
	 * 获取指定抽奖活动下积分最低的积分奖品
	 * @param activityId 抽奖活动ID
	 * @return 返回积分最低的积分奖品
	 */
	@Override
	public DrawLotteryCreditGoodsInfo queryMinByActivityId(Integer activityId) {
		if (activityId == null) {
			return null;
		}
		return queryMin(activityId);
	}

	/**
	 * 进行抽奖
	 * @param activityId 抽奖活动的唯一ID
	 * @return 抽中的积分奖品，未抽中返回null
	 */
	@Override
	public DrawLotteryCreditGoodsInfo draw(Integer activityId) {
		if (activityId == null) {
			return null;
		}
		List<DrawLotteryCreditGoodsInfo> drawLotteryCreditGoodsInfoList = queryByActivityId(activityId);
		int random = (int) Math.floor(Math.random() * 100);
		DrawLotteryCreditGoodsInfo drawLotteryCreditGoodsInfo = null;
		for (DrawLotteryCreditGoodsInfo goods : drawLotteryCreditGoodsInfoList) {
			if (random < goods.getProbability()) {
				drawLotteryCreditGoodsInfo = goods;
				break;
			} else {
				random -= goods.getProbability();
			}
		}
		return drawLotteryCreditGoodsInfo;
	}
	
	/**
	 * 获取抽奖活动下的积分商品列表
	 * @param activityId 抽奖活动ID
	 * @return 积分商品列表
	 */
	private DrawLotteryCreditGoodsInfo queryMin(Integer activityId) {
		String creditGoodsKey = DRAW_LOTTERY_CREDIT_GOODS_INFO_PREFIX + activityId;
		List<DrawLotteryCreditGoodsInfo> creditGoodsInfoList = null;
		DrawLotteryCreditGoodsInfo minCreditGoods = null;
		if (redis.exists(creditGoodsKey)) {
			creditGoodsInfoList = redis.lrange(creditGoodsKey, 0, -1, DrawLotteryCreditGoodsInfo.class);
			for (DrawLotteryCreditGoodsInfo creditGoods: creditGoodsInfoList) {
				if (minCreditGoods == null || creditGoods.getCredit() < minCreditGoods.getCredit()) {
					minCreditGoods = creditGoods;
				}
			}
		} else {
			minCreditGoods = drawLotteryCreditGoodsInfoDao.queryMinByActivityId(activityId);
		}
		return minCreditGoods;
	}
	
	/**
	 * 获取抽奖活动下积分最少的的积分商品
	 * @param activityId 抽奖活动ID
	 * @return 积分最少的积分商品
	 */
	private List<DrawLotteryCreditGoodsInfo> queryByActivityId(Integer activityId) {
		String creditGoodsKey = DRAW_LOTTERY_CREDIT_GOODS_INFO_PREFIX + activityId;
		List<DrawLotteryCreditGoodsInfo> creditGoodsInfoList = null;
		if (redis.exists(creditGoodsKey)) {
			creditGoodsInfoList = redis.lrange(creditGoodsKey, 0, -1, DrawLotteryCreditGoodsInfo.class);
		} else {
			creditGoodsInfoList = drawLotteryCreditGoodsInfoDao.queryByActivityId(activityId);
			if (creditGoodsInfoList != null && !creditGoodsInfoList.isEmpty()) {
				redis.rpush(creditGoodsKey, creditGoodsInfoList);
			}
		}
		return creditGoodsInfoList;
	}

}
